<?
###########################################################################################
class gd{
var $grandeW=500;
var $grandeH=500;
var $medioW=105;
var $medioH=105;
var $thumbW=40;
var $thumbH=40;
var $valores=array();
var $imgUpload=array();
var $nomeSite="";

	function gd(){
	}
	
###########################		IMAGENS		###########################
	function colocaNome($destino){
		$tam=(strlen($this->nomeSite)*5+2);
		$pars=getimagesize($destino);
		$image  = $this->criaImagem($pars[2],$destino);
		$text_color = imagecolorallocate($image, 255, 255,255);
		imagestring($image, 1, $pars[0]-$tam, $pars[1]-10,  $this->nomeSite, $text_color);
		$this->retornaImagem($pars[2],$image,$destino);
		imagedestroy($image);
	}
	
	function criaImagem($tipo,$fonte){
		switch($tipo){
			case 1:
				return imagecreatefromgif($fonte);
			break;
			case 2:
				return imagecreatefromjpeg($fonte);
			break;
			case 3:
				return imagecreatefrompng($fonte);
			break;
			default:
				return false;
			break;
		}
	}
	
	function redimensionaImagemProporcional ($fonte,$destino,$maxLargura,$maxAltura){
		if(file_exists($fonte)){
		list($largOrig, $altOrig, $tipOrig) = getimagesize($fonte);
			if ($largOrig < $altOrig) {
			$maxLargura = ($maxAltura / $altOrig) * $largOrig;
			} 
			else {
			$maxAltura = ($maxLargura / $largOrig) * $altOrig;
			}
		$image_p = imagecreatetruecolor($maxLargura, $maxAltura);
		$imagem  = $this->criaImagem($tipOrig,$fonte);
		imagecopyresampled($image_p, $imagem, 0, 0, 0, 0, $maxLargura, $maxAltura, $largOrig, $altOrig);
		$image_p=$this->UnsharpMask($image_p,80,0.5,3);
		$this->retornaImagem($tipOrig,$image_p,$destino);
		imagedestroy($image_p);
			if($maxLargura==$this->grandeW || $maxAltura==$this->grandeH){
			$this->colocaNome($destino);
			}
		}
		else{
		return false;
		}
	}

	function redimensionaImagemRecorte ($imagem, $thumbnail, $x , $y) {	
	$pars=getimagesize($imagem);
	$imagem_orig  = $this->criaImagem($pars[2],$imagem);
	$pontoX          =   ImagesX($imagem_orig);
	$pontoY          =   ImagesY($imagem_orig);
	$rx=0;
	$ry=0;
	$imx=($pontoX/$x);
	$imy=($pontoY/$y);
		if($imx>$imy){
		$ty=$pontoY;
		$tx=($ty*($x/$y));
		$rx=(($pontoX-$tx)/2);
		}
		else{
		$tx=$pontoX;
		$ty=($tx*($y/$x));
		$ry=(($pontoY-$ty)/2);
		}
	$imagem_fin      =   ImageCreateTrueColor($x, $y );
	ImageCopyResampled($imagem_fin, $imagem_orig, 0, 0, $rx, $ry, $x+1, $y+1, $tx, $ty);
	$imagem_fin=$this->UnsharpMask($imagem_fin,80,0.5,3);
	$this->retornaImagem($pars[2],$imagem_fin, $thumbnail);
	ImageDestroy($imagem_orig);
	ImageDestroy($imagem_fin);
				if($x==$this->grandeW || $y==$this->grandeH){
				$this->colocaNome($thumbnail);
				}
	return true;
	}
	
	function retornaImagem($tipo,$fonte,$destino){
		switch($tipo){
			case 1:
				return imagegif($fonte,$destino);
			break;
			case 2:
				return imagejpeg($fonte,$destino,65);
			break;
			case 3:
				return imagepng($fonte,$destino);
			break;
			default:
				return false;
			break;
		}
	}
	
	function sobeArquivo($numArq){
	$this->localArquivos=$this->uploaddir.($_POST['ima_sessao']==""?$_GET['idpagina']:$_POST['ima_sessao']);
		for($x=0;$x<count($this->arquivos);$x++){
			if($_FILES[$this->arquivos[$x]]["size"]>0){ // Arquivo para subir
			$a=explode(".",$_FILES[$this->arquivos[$x]]["name"]);
			$ext=strtolower($a[count($a)-1]);
			$local=$this->localArquivos."/originais/".$this->arquivos[$x]."_".$numArq."OR.".$ext;
			$local1=$this->localArquivos."/".$this->arquivos[$x]."_".$numArq."TN.".$ext;
			$local2=$this->localArquivos."/".$this->arquivos[$x]."_".$numArq."MD.".$ext;
			$local3=$this->localArquivos."/".$this->arquivos[$x]."_".$numArq."GR.".$ext;
			move_uploaded_file($_FILES[$this->arquivos[$x]]['tmp_name'],$local);
			$this->imgUpload[]=$local;
				if($ext=="jpg"||$ext=="png"||$ext=="gif"){
				//	redimensionaImagemProporcional($fonte,$destino,$maxLargura,$maxAltura)
				//	redimensionaImagemRecorte	($imagem, $thumbnail, $x , $y)
				$this->redimensionaImagemRecorte($local,$local1,$this->thumbW,$this->thumbH);
				$this->redimensionaImagemRecorte($local,$local2,$this->medioW,$this->medioH);
				$this->redimensionaImagemProporcional($local,$local3,$this->grandeW,$this->grandeH);
				}
			}
		}
	}
	
	function UnsharpMask($img, $amount, $radius, $threshold)    {  //objeto imagem, 0 a 200, 0 a 1.0, 0 a 5.0 
		if ($amount > 500)    $amount = 500;  
		$amount = $amount * 0.016;  
		if ($radius > 50)    $radius = 50;  
		$radius = $radius * 2;  
		if ($threshold > 255)    $threshold = 255;  
		  
		$radius = abs(round($radius));     // Only integers make sense.  
		if ($radius == 0) {  
			return $img; imagedestroy($img); break;        }  
		$w = imagesx($img); $h = imagesy($img);  
		$imgCanvas = imagecreatetruecolor($w, $h);  
		$imgBlur = imagecreatetruecolor($w, $h);  
	
		if (function_exists('imageconvolution')) { // PHP >= 5.1   
				$matrix = array(   
				array( 1, 2, 1 ),   
				array( 2, 4, 2 ),   
				array( 1, 2, 1 )   
			);   
			imagecopy ($imgBlur, $img, 0, 0, 0, 0, $w, $h);  
			imageconvolution($imgBlur, $matrix, 16, 0);   
		}   
		else {   
			for ($i = 0; $i < $radius; $i++)    {  
				imagecopy ($imgBlur, $img, 0, 0, 1, 0, $w - 1, $h); // left  
				imagecopymerge ($imgBlur, $img, 1, 0, 0, 0, $w, $h, 50); // right  
				imagecopymerge ($imgBlur, $img, 0, 0, 0, 0, $w, $h, 50); // center  
				imagecopy ($imgCanvas, $imgBlur, 0, 0, 0, 0, $w, $h);  
	
				imagecopymerge ($imgBlur, $imgCanvas, 0, 0, 0, 1, $w, $h - 1, 33.33333 ); // up  
				imagecopymerge ($imgBlur, $imgCanvas, 0, 1, 0, 0, $w, $h, 25); // down  
			}  
		}  
	
		if($threshold>0){  
			for ($x = 0; $x < $w; $x++)    { // each row  
				for ($y = 0; $y < $h; $y++)    { // each pixel  
						  
					$rgbOrig = ImageColorAt($img, $x, $y);  
					$rOrig = (($rgbOrig >> 16) & 0xFF);  
					$gOrig = (($rgbOrig >> 8) & 0xFF);  
					$bOrig = ($rgbOrig & 0xFF);  
					  
					$rgbBlur = ImageColorAt($imgBlur, $x, $y);  
					  
					$rBlur = (($rgbBlur >> 16) & 0xFF);  
					$gBlur = (($rgbBlur >> 8) & 0xFF);  
					$bBlur = ($rgbBlur & 0xFF);  
					$rNew = (abs($rOrig - $rBlur) >= $threshold)   
						? max(0, min(255, ($amount * ($rOrig - $rBlur)) + $rOrig))   
						: $rOrig;  
					$gNew = (abs($gOrig - $gBlur) >= $threshold)   
						? max(0, min(255, ($amount * ($gOrig - $gBlur)) + $gOrig))   
						: $gOrig;  
					$bNew = (abs($bOrig - $bBlur) >= $threshold)   
						? max(0, min(255, ($amount * ($bOrig - $bBlur)) + $bOrig))   
						: $bOrig;  
					  
					if (($rOrig != $rNew) || ($gOrig != $gNew) || ($bOrig != $bNew)) {  
							$pixCol = ImageColorAllocate($img, $rNew, $gNew, $bNew);  
							ImageSetPixel($img, $x, $y, $pixCol);  
						}  
				}  
			}  
		}  
		else{  
			for ($x = 0; $x < $w; $x++)    { // each row  
				for ($y = 0; $y < $h; $y++)    { // each pixel  
					$rgbOrig = ImageColorAt($img, $x, $y);  
					$rOrig = (($rgbOrig >> 16) & 0xFF);  
					$gOrig = (($rgbOrig >> 8) & 0xFF);  
					$bOrig = ($rgbOrig & 0xFF);  
					  
					$rgbBlur = ImageColorAt($imgBlur, $x, $y);  
					  
					$rBlur = (($rgbBlur >> 16) & 0xFF);  
					$gBlur = (($rgbBlur >> 8) & 0xFF);  
					$bBlur = ($rgbBlur & 0xFF);  
					  
					$rNew = ($amount * ($rOrig - $rBlur)) + $rOrig;  
						if($rNew>255){$rNew=255;}  
						elseif($rNew<0){$rNew=0;}  
					$gNew = ($amount * ($gOrig - $gBlur)) + $gOrig;  
						if($gNew>255){$gNew=255;}  
						elseif($gNew<0){$gNew=0;}  
					$bNew = ($amount * ($bOrig - $bBlur)) + $bOrig;  
						if($bNew>255){$bNew=255;}  
						elseif($bNew<0){$bNew=0;}  
					$rgbNew = ($rNew << 16) + ($gNew <<8) + $bNew;  
						ImageSetPixel($img, $x, $y, $rgbNew);  
				}  
			}  
		}  
		imagedestroy($imgCanvas);  
		imagedestroy($imgBlur);  
		return $img;  
	}
}
?>